package cc.messcat.web.log;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.constant.CommonRedisConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.entity.log.Log;
import cc.messcat.entity.system.McUser;
import cc.messcat.log.OperationLog;
import cc.messcat.service.log.LogService;
import cc.messcat.service.system.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/log")
public class LogController {

	@Resource
	private LogService logService;

	@Autowired
	private UserService userService;

	/**
	 * 日志条件查询
	 * @author Fog
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/listLog")
	@ResponseBody
	public ResponseBean listLog(Integer pageNo, Integer pageSize,
		Log log,String start, String end,HttpSession session) throws ParseException {
		McUser user = userService.retrieveUserByUsername((String) session.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER));

		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,
			logService.listLog(user,pageNo,pageSize,log,start,end));
	}

	/**
	 * 日志条件导出
	 * @author Fog
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/exportLog")
	@ResponseBody
	public ResponseBean exportLog(Integer pageNo, Integer pageSize, 
		Log log,String start, String end,HttpSession session,HttpServletResponse response) throws ParseException {
		McUser user = userService.retrieveUserByUsername((String) session.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER));
		try {
			HSSFWorkbook wb = logService.exportLog(user,pageNo,pageSize,log, start, end);
			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode("日志记录表", "UTF-8") + ".xls");
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");

			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
			wb.write(bufferedOutputStream);
			bufferedOutputStream.close();

			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXPORT_SUCCESS);
		} catch (IOException e) {
			e.printStackTrace();
			return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_EXPORT_FAIL);
		}
	}
	
	/**
	 * 日志条件删除
	 * @author Fog
	 * @throws ParseException 
	 */
	@OperationLog(desc="日志删除",module="日志")
	@RequestMapping(value = "/removeLog")
	@ResponseBody
	public ResponseBean removeLog(Log log,String ids,String start, String end,HttpSession session) throws ParseException {
		McUser user = userService.retrieveUserByUsername((String) session.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER));
		
		//防清空
		if(StringUtils.isBlank(log.getDescription()) 
			&& StringUtils.isBlank(log.getUserName())
			&& StringUtils.isBlank(log.getRequestIp())
			&& log.getId() == null
			&& StringUtils.isBlank(ids)
			&& StringUtils.isBlank(start)
			&& StringUtils.isBlank(end)) {
			
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, "未删除任何记录",null);
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logService.removeLog(user,log,ids,
			dateFormat.parse(StringUtils.isBlank(start) ? dateFormat.format(new Date(0)) : start),
			dateFormat.parse(StringUtils.isBlank(end) ? dateFormat.format(new Date()) : end));
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_SUCCESS,null);
	}
	
	/**
	 * 日志查询
	 */
	@RequestMapping(value = "/findList",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean findList(Integer pageNo, Integer pageSize, Log log) {
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,
			logService.retrieveByObject(pageSize, pageNo, null, log, Log.class.getName(),"id","desc",null,null));
	}
	
	//查询全部
	@RequestMapping(value = "/findAllList",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean findAllList(Integer pageNo, Integer pageSize) {
		if (pageNo==null||pageNo<1){
			pageNo=1;
		}
		if (pageSize==null||pageSize<1){
			pageSize=10;
		}
		Pager pager = this.logService.getPagerfindAllList(pageNo, pageSize);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,pager);
	}
	
	@RequestMapping(value = "/find",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean find(Long logId){
		if(logId==null){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, CommonConstant.MSG_PARAMETER_EMPTY);
		}
		Log log = logService.retrieveObject(logId);
		if(log==null){
			return new ResponseBean(CommonConstant.SUCCESS_CODE_300, "日志不存在");
		}
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, log);
	}

	@RequestMapping(value = "/addlog",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean addlog(Log log){
	/*	if(logId==null){
			return new ResponseBean(CommonConstant.FAIL_CODE_400, CommonConstant.MSG_PARAMETER_EMPTY);
		}
		Log log = logService.retrieveObject(logId);
		if(log==null){
			return new ResponseBean(CommonConstant.SUCCESS_CODE_300, "日志不存在");
		}*/
		/*log.setAddTime(new Date());
		log.setDescription("123");
		log.setMethod("666");
		log.setParams("0");
		log.setRequestIp("192.168.0.87");
		log.setUserName("admin");*/
		logService.addlog(log);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS, log);
	}
}
